package main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.StringWriter;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import model.ReadResultModel;
import parse.ParseCore;
import parse.impl.ParseToJBO;
import parse.impl.ParseToSQL;
import parse.impl.ParseToXML;
import reader.ReaderCore;
import reader.impl.CSVSourceReader;
import reader.impl.XLSSourceReader;
import reader.impl.XMLConfigReader;

public class WindowFramework extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final String[] PARSETYPE = new String[] { "转换为输出配置文件", "转换为SQL CREATE TABLE", "转换为JBO配置文件" };
	private final String[] SUBLISTTYPE = new String[] { "类型(长度)", "类型与长度分开" };

	private JButton selectCsvPathButton;
	private JButton confirmButton;
	private JTextArea parseResultTextArea;
	private JTextField filePathTextFiled;
	private JTextField tableName;
	private JFileChooser jfChooser;
	private JComboBox<String> parseType;
	private JComboBox<String> subListType;

	public WindowFramework() {
		super();
		this.setTitle("AmarQuickParse");
		JPanel jPanel = new JPanel();
		placeComponents(jPanel);
		this.add(jPanel);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(600, 400);
		this.setVisible(true);
	}

	private void placeComponents(JPanel jPanel) {
		
		jPanel.setLayout(null);
		
		JLabel csvPathLabel = new JLabel("表格文件的路径：");
		csvPathLabel.setBounds(10, 20, 110, 25);
		jPanel.add(csvPathLabel);

		filePathTextFiled = new JTextField(25);
		filePathTextFiled.setBounds(140, 20, 200, 25);
		jPanel.add(filePathTextFiled);

		selectCsvPathButton = new JButton("浏览...");
		selectCsvPathButton.addActionListener(this);
		selectCsvPathButton.setBounds(360, 20, 80, 25);
		jPanel.add(selectCsvPathButton);

		confirmButton = new JButton("确定");
		confirmButton.addActionListener(this);
		confirmButton.setBounds(450, 20, 80, 25);
		jPanel.add(confirmButton);

		parseType = new JComboBox<String>(PARSETYPE);
		parseType.setBounds(10, 50, 120, 25);
		parseType.setSelectedIndex(0);
		jPanel.add(parseType);

		subListType = new JComboBox<String>(SUBLISTTYPE);
		subListType.setBounds(150, 50, 120, 25);
		subListType.setSelectedIndex(0);
		jPanel.add(subListType);

		JLabel tableNameLabel = new JLabel("SQL表名：");
		tableNameLabel.setBounds(280, 50, 80, 25);
		jPanel.add(tableNameLabel);

		tableName = new JTextField(10);
		tableName.setBounds(360, 50, 170, 25);
		jPanel.add(tableName);

		parseResultTextArea = new JTextArea(10, 40);
		parseResultTextArea.setLineWrap(true);
		parseResultTextArea.setBounds(10, 80, 165, 100);
		parseResultTextArea.setLineWrap(false);
		JScrollPane scrollPane = new JScrollPane(parseResultTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(10, 80, 520, 250);
		jPanel.add(scrollPane);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton jButton = (JButton) e.getSource();
		if (jButton == selectCsvPathButton) {
			jfChooser = new JFileChooser(new File("."));
			int status = jfChooser.showOpenDialog(this);
			if (status == JFileChooser.APPROVE_OPTION) {
				File file = jfChooser.getSelectedFile();
				filePathTextFiled.setText(file.getPath());
			}
		}
		if (jButton == confirmButton) {
			XMLWriter xmlWriter = null;
			StringWriter stringWriter = null;
			OutputFormat outputFormat = null;
			try {
				//读取配置
				XMLConfigReader configReader = new XMLConfigReader();
				configReader.readFile("config/config.xml", 0);
				
				String filePath = filePathTextFiled.getText();
				
				ReaderCore sourceReader = null;
				String fileType = filePath.substring(filePath.lastIndexOf('.'));
				switch (fileType) {
				case ".csv":
					sourceReader = new CSVSourceReader();
					break;
				case ".xls":
				case ".xlsx":
					sourceReader = new XLSSourceReader();
					break;
				default:
					throw new Exception("不支持此文件类型");
				}
				ReadResultModel readResultModel = (ReadResultModel) sourceReader.readFile(filePath, subListType.getSelectedIndex());
				
				ParseCore parser = null;
				switch (parseType.getSelectedIndex()) {
				case 0:
					parser = new ParseToXML();
					break;
				case 1:
					parser = new ParseToSQL();
					break;
				case 2:
					parser = new ParseToJBO();
					break;
				default:
					throw new Exception("不支持转换为此类型");
				}
				Object parseResult = parser.doParse(readResultModel, tableName.getText(), subListType.getSelectedIndex());
				if (parseResult instanceof Document) {
					stringWriter = new StringWriter();
					outputFormat = OutputFormat.createPrettyPrint();
					outputFormat.setEncoding("utf-8");
					xmlWriter = new XMLWriter(stringWriter, outputFormat);
					xmlWriter.write((Document)parseResult);
					parseResultTextArea.setText(stringWriter.toString());
				}
				if (parseResult instanceof String) {
					parseResultTextArea.setText((String)parseResult);
				}
			} catch (Exception exception) {
				JOptionPane.showMessageDialog(this, exception.getMessage() + exception.getStackTrace());
			}
		}
	}
}
